package com.swyc.osschat.Common.Util.System;

import org.springframework.util.ResourceUtils;

import java.io.File;
import java.util.Properties;

public class SystemUtil {
    /**
     * 判断服务器是不是linux
     * @return
     */
    public static boolean isOsLinux(){
        Properties prop = System.getProperties();
        String os = prop.getProperty("os.name");
        if (os != null && os.toLowerCase().indexOf("linux") > -1) {
            return true;
        } else {
            return false;
        }
//        return sun.plugin2.util.SystemUtil.isWindowsVista();
    }

    /**
     * 获取linux根目录
     * @return
     */
    public static String getLinxuHome(){
        String classPath = Thread.currentThread().getContextClassLoader()
                .getResource("").getPath();
        String rootPath = "";
        /** For Linux */
        if ("/".equals(File.separator)) {
            String path = classPath.substring(0,
                    classPath.indexOf("/WEB-INF/classes"));
            rootPath = path.substring(0, path.lastIndexOf("/"));
            rootPath = rootPath.replace("\\", "/");
        }
        return rootPath;
    }

    /**
     * 获取项目根目录
     * @return
     */
    public static String getRootPath() {
        String classPath = Thread.currentThread().getContextClassLoader()
                .getResource("").getPath();
        String rootPath = "";
        /** For Windows */
        if ("\\".equals(File.separator)) {
            String path = classPath.substring(1,
                    classPath.indexOf("/WEB-INF/classes"));
            rootPath = path.substring(0, path.lastIndexOf("/"));
            rootPath = rootPath.replace("/", "\\");
        }
        rootPath = getLinxuHome();
        return rootPath;
    }

    /**
     * 获取项目路径
     * @return
     * @throws Exception
     */
    public static String javaHomePath() throws Exception {
        File path = new File(ResourceUtils.getURL("classpath:").getPath());
        if(!path.exists()) {
            path = new File("");
        }
        return path.getPath();
    }
}
